package com.itheima.service;

import com.itheima.R;
import com.itheima.api.SmsHomeNewProductApi;
import com.itheima.exception.ConsumerException;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.pojo.SmsHomeNewProduct;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pk
 * @date 2022/10/13/18:01
 */
@Service
public class SmsHomeNewProductService {

    @DubboReference
    private SmsHomeNewProductApi newProductApi;

    /**
     * 添加首页新品
     * @param newProduct
     */
    public void createNewProduct(SmsHomeNewProduct newProduct) {
        newProductApi.save(newProduct);
    }


    /**
     * 分页查询新品
     * @param productName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    public PageResult findNewProduct(String productName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        return newProductApi.findNewProduct(productName,pageNum,pageSize,recommendStatus);
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param sort
     */
    public void updateSortByid(Long id, Integer sort) {
        //根据id查询品牌实例
        SmsHomeNewProduct newProduct=newProductApi.findBrandById(id);
        if (newProduct!=null){
            if (sort!=null){
                newProduct.setSort(sort);
            }
        }
        //调用api修改
        newProductApi.updateSortByid(newProduct);
    }

    /**
     * 批量删除新品
     * @param id
     */
    public void deleteNewProduct(Integer[] id) {
        List<Long> ids = Arrays.stream(id).map(e->Long.valueOf(e)).collect(Collectors.toList());
        //调用api删除
        if (!newProductApi.deleteNewProduct(ids)){
            //删除失败则抛出异常
            throw new ConsumerException("网络繁忙请刷新重试");
        }
    }

    /**
     * 批量修改新品
     * @param id
     * @param recommendStatus
     */
    public void updateRecommendStatus(Integer[] id, Integer recommendStatus) {
        List<Long> ids = Arrays.stream(id).map(e->Long.valueOf(e)).collect(Collectors.toList());
        //调用api修改
        if (!newProductApi.updateRecommendStatus(ids,recommendStatus)){
            //修改失败则抛出异常
            throw new ConsumerException("网络繁忙请刷新重试");
        }
    }
}
